/**
 * 
 */
package SQLDAL;

import Acquaintance.IACluster;
import DALFactory.IDataProcess;
import Entity.EClusterResult;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;

/**
 *SQL 数据层用于实现IDataProcess类
 * @author Administrator
 */
public class SQLDataProcess implements IDataProcess {
/**
 * 
 */
    public void removeAllCluster() {
        SQLStatement connStr = new SQLStatement();
        SQLConnection conn = connStr.openSQLConnection();
        String delCmd = "delete [RH].[dbo].[Cluster]";
        String selcmd2 = "select top 1 * from [RH].[dbo].[Cluster]";

        ResultSet rs2 = connStr.executeQuery(conn, selcmd2);
        try {
            if (rs2.next()) {
                connStr.executeUpdate(conn, delCmd);
            }
        } catch (Exception e) {
            e.printStackTrace();
            conn.closeConnection();
        }
        conn.closeConnection();

    }
/**
 * 
 * @param map 
 */
    public void PushClusterIntoDB(TreeMap map) {
        SQLStatement connStr = new SQLStatement();
        SQLConnection conn = connStr.openSQLConnection();
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            IACluster cluster = (IACluster) it.next();
            String statementStr = "insert into [RH].[dbo].[Cluster](Parent,Value,ChildClass1,ChildClass2) values(" + cluster.getEparent() + "," + cluster.getEvalue() + "," + cluster.getEchild1() + "," + cluster.getEchild2() + ");";
            connStr.executeUpdate(conn, statementStr);
        }
    }
/**
 * 
 * @return 
 */
    public boolean ClusterIsEmty() {
        SQLStatement connStr = new SQLStatement();
        SQLConnection conn = connStr.openSQLConnection();

        String selcmd2 = "select top 1 * from [RH].[dbo].[Cluster]";

        ResultSet rs2 = connStr.executeQuery(conn, selcmd2);
        try {
            if (rs2.next()) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            conn.closeConnection();
            return true;
        }
    }
/**
 * 
 * @return 
 */
    public TreeMap<Integer, IACluster> getParentMap() {
        TreeMap<Integer, IACluster> map = new TreeMap<Integer, IACluster>();
        SQLStatement connStr = new SQLStatement();
        SQLConnection conn = connStr.openSQLConnection();
        String selcmd = "select * from [RH].[dbo].[Cluster]";
        ResultSet rs = connStr.executeQuery(conn, selcmd);
        try {
            while (rs.next()) {
                IACluster cluster = mapIACluster(rs);
                map.put(cluster.getEparent(), cluster);
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            conn.closeConnection();
        }
        return map;
    }
/**
 * 
 * @return 
 */
    public HashMap<Integer, IACluster> getChildenMap() {
        HashMap<Integer, IACluster> map=new HashMap<Integer, IACluster>();
        SQLStatement connStr = new SQLStatement();
        SQLConnection conn = connStr.openSQLConnection();
        String selcmd = "select * from [RH].[dbo].[Cluster]";
        ResultSet rs = connStr.executeQuery(conn, selcmd);
        try {
            while (rs.next()) {
                IACluster cluster = mapIACluster(rs);
                map.put(cluster.getEchild1(), cluster);
                map.put(cluster.getEchild2(), cluster);
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            conn.closeConnection();
        }
        return map;
    }
/**
 * 
 * @param rs
 * @return
 * @throws SQLException 
 */
    public IACluster mapIACluster(ResultSet rs) throws SQLException {
        IACluster cluster = new EClusterResult();
        cluster.setEchild1(rs.getInt("ChildClass1"));
        cluster.setEchild2(rs.getInt("ChildClass2"));
        cluster.setEparent(rs.getInt("Parent"));
        cluster.setEvalue(rs.getInt("Value"));
        return cluster;
    }
}
